perm filename A81.TEX[106,RWF] blob
sn#825564 filedate 1986-10-03 generic text, type C, neo UTF8
COMMENT ⊗ VALID 00004 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 \magnification\magstephalf
C00014 00003 \line{\bf I've Been Workin' On the Railroad.\hfill}
C00024 00004 \line{\bf Expressions (Formulas): I.\hfill}
C00035 ENDMK
C⊗;
\magnification\magstephalf
\input macro.tex
\def\today{\ifcase\month\or
January\or February\or March\or April\or May\or June\or
July\or August\or September\or October\or November\or December\fi
\space\number\day, \number\year}
\baselineskip 14pt
\rm
\line{\sevenrm a81.tex[106,phy] \today\hfill}
\font\rmn=cmr9 %ninepoint
\font\tte=cmtt8
\def\eol{\mskip1mu\raise2pt\vbox{\hbox{\tte E}}
\raise1pt\vbox{\hbox{\tte O}}
\hbox{\tte L}\mskip1mu}
\bigskip
\line{{\bf Catch a Falling Star.} Simple Programs.\hfill}
The simplest Pascal program known to man is this:
\medskip
{\obeylines
\obeyspaces
\let =\
\tt
\undertext{PROGRAM} STUPID;
\undertext{BEGIN}
\undertext{END}.
}
\noindent
It is useless, because it doesn't do anything, but it illustrates the overall
framework of every Pascal program.
There is a heading consisting of the word ``{\tt PROGRAM}'' followed by a freely
chosen internal
name for the program, and separated by a semicolon from the rest of the
program. There is a list of internal names for files
used by the program (empty in this
example). There is a list of actions to be done by the program (empty in this
example) surrounded by {\tt BEGIN} {\tt ...} {\tt END}. Last, there is a period.
Several words, like {\tt PROGRAM}, {\tt BEGIN}, and {\tt END} have fixed meanings
and usages in Pascal.
When such a word is introduced in this text, it is underlined. Appendix \_\_\_ lists
all of these {\sl reserved words\/}.
To be useful, a program must produce some information, usually by printing it.
One way to do this is to have the program place information into an electronic
storage medium called a {\sl file\/}, for later printing on paper. The internal
names of external
files used by the program must be listed in the program heading. There are
simplified rules for placing information into a file if it is called {\tt OUTPUT},
so most examples in this book use a file of that name, and begin with
the heading
\medskip
{\obeylines\obeyspaces\let =\ \tt
PROGRAM${\cal N}$ (OUTPUT);
}
\medskip
\noindent where $\cal N$ can be any name (except for reserved words).
The actions performed by the program appear between {\tt BEGIN} and {\tt END},
separated by semicolons. Actions which can occur there are called (slightly
misleadingly) {\sl statements\/}. One form of statement prints a sequence of
characters of the sort found on a typewriter keyboard:
\medskip
{\obeylines
\obeyspaces
\let =\
\tt
WRITE('MARCH 12, 1985')
}
\medskip
\noindent
puts the fifteen letters, spaces, digits, and punctuation marks enclosed between
the two apostrophes into the file called {\tt OUTPUT}, for eventual printing.
Any sequence of keyboard characters (except for apostrophes) can be used.
When the resulting {\tt OUTPUT} file is printed, the paper will appear as
\smallskip
\def\boxit#1{\vbox{\hrule\hbox{\vrule\kern3pt
\vbox{\kern3pt#1\kern3pt}\kern3pt\vrule}\hrule}}
\setbox1=\vbox{\hsize 23pc \noindent \strut
{\obeylines\obeyspaces\let =\ \tt
MARCH 12, 1985 \phantom{0}
\phantom{ }
}
\strut}
\qquad\boxit{\box1}
\smallskip
The complete program to create the printing shown above could take any of the
forms
\medskip
{\obeylines\obeyspaces\let =\ \tt
PROGRAM SIMPLE(OUTPUT);
BEGIN
WRITE('MARCH 12, 1985')
END.
}
\noindent
or
{\obeylines
\obeyspaces
\let =\
\tt
PROGRAM SIMPLE(OUTPUT); BEGIN WRITE
('MARCH 12, 1985') END.
}
\noindent
or
{\obeylines
\obeyspaces
\let =\
\tt
PROGRAM SIMPLE(OUTPUT);
BEGIN
WRITE('MARCH ');
WRITE('12, ');
WRITE('1985')
END.
}
\medskip
\noindent
The choice between the above forms is made stylistically. In later pages, we
will discuss ways to make a large and complicated program easy to read and
modify. For the moment, any placement of the program on the page will work if
you avoid breaking (with spaces or new lines) any words, numbers, or quoted
material. In most cases, if the program layout looks reasonable it will work.
A program can calculate numbers and place them on the output file. To calculate
the date when Halley's comet will return, I~can use the formula {\tt 1985+71} in
a {\tt WRITE} command, without any quotation marks around it:
\medskip
{\obeylines
\obeyspaces
\let =\
\tt
WRITE(1985+71)
}
\medskip
\noindent
which prints
\smallskip
\def\boxit#1{\vbox{\hrule\hbox{\vrule\kern3pt
\vbox{\kern3pt#1\kern3pt}\kern3pt\vrule}\hrule}}
\setbox2=\vbox{\hsize 23pc \noindent \strut
{\obeylines\obeyspaces\let =\ \tt
2056 \phantom{0}
\phantom{ }
}
\strut}
\qquad\boxit{\box2}
\smallskip
A {\tt WRITE} command can print several items separated by commas, each of
which can be a formula or a quoted sequence of characters
\smallskip
{\obeylines\obeyspaces\let =\ \tt
PROGRAM COMETYEAR(OUTPUT);
BEGIN
WRITE('HALLEY''S COMET WILL RETURN IN',1985+71,'IF NOTHING HAPPENS
TO PREVENT IT')
END.
}
\medskip
\def\boxit#1{\vbox{\hrule\hbox{\vrule\kern3pt
\vbox{\kern3pt#1\kern3pt}\kern3pt\vrule}\hrule}}
\setbox3=\vbox{\hsize 31pc \noindent \strut
{\obeylines\obeyspaces\let =\ \tt
HALLEY'S COMET WILL RETURN IN 2056 IF NOTHING HAPPENS TO PREVENT IT
\phantom{ }
}
\strut}
\qquad\boxit{\box3}
\smallskip
\noindent
The apostrophe in {\tt HALLEY'S} is doubled in the program
to distinguish it from the one that ends the quoted material.
A program that writes more than a hundred or so characters
may create an output line which\rlap{ on}
printing falls off the end of the page
like the line above. Some printers will automatically
break such a long line, others will
not, so a programmer should not permit lines longer than the printer allows
(typically about 80~characters for letter size paper and 132~characters for
wide fanfold paper). To end a line on the output file, use the command
{\tt WRITELN} (pronounced ``write line''), which places a special end-of-line
symbol (not called a character) in the output file. The symbol is not visible,
but we use the visible symbol~$\eol$ to represent it in this text. The program
fragment
\medskip
{\obeylines
\obeyspaces
\let =\
\tt
WRITE('ABC'); WRITELN; WRITE('DE'); WRITELN; WRITE('F')
}
\medskip
\noindent
produces the file
\medskip
{\obeylines
\obeyspaces
\let =\
\tt
ABC $\eol$ DE $\eol$ F
}
\medskip
\noindent
which, when printed, becomes
\bigskip
{\obeylines
\obeyspaces
\let =\
\tt
ABC
DE
F
}
\vskip .5truein
\noindent
Often it is convenient to combine {\tt WRITE(x,y,z)} with {\tt WRITELN} as
{\tt WRITELN(x,y,z)}; the program fragment above can be written
\medskip
{\obeylines
\obeyspaces
\let =\
\tt
WRITELN('ABC');
WRITELN('DE');
WRITE('F')
}
\medskip
\noindent
Notice that whether the statements are on one line or several has no effect
on the line breaks on the output page, which are controlled only by
{\tt WRITELN}s in the program.
\vfill\eject
\line{\bf I've Been Workin' On the Railroad.\hfill}
As a compact way to convey definitions of the forms of language allowed in
Pascal programs, we use diagrams suggestive of railroad tracks. If you follow
the tracks from entrance to exit in the direction of the arrows, you will be
tracing a legitimate form.
\bigskip
\halign{\qquad\qquad
#\hfil\qquad\hfil\qquad\qquad\hfil\qquad\qquad\hfil\qquad\qquad%
\hfil\qquad\qquad\hfil\qquad\qquad\hfil\cr
&&&&\hfil{,}\cr
\noalign{\smallskip}
statement\cr
&{\tt WRITE}&(&$\bullet$&formula $=$&$\bullet$&)\cr
&&&&expression\cr
\noalign{\bigskip\medskip}
&&&"ed stuff $=$\cr
&&&&string constant\cr}
\bigskip\bigskip
\noindent
In the above (simplified) railroad track diagram, we can follow the arrows through
the form
\medskip
{\obeylines
\obeyspaces
\let =\
\tt
WRITE({\rm quoted stuff, formula, quoted stuff})
}
\medskip
\noindent
as used in the {\tt COMETYEAR} program. The dotted lines show where
alternatives have been omitted until later; there are other kinds of statements,
and there are other options that can appear in {\tt WRITE} statements.
Some boxes contain two words or phrases; the first is explanatory, the
second is a technical term under which the allowed choices are defined
elsewhere. For example, formulas may be printed by a {\tt WRITE} statement;
to find exactly what formulas are permitted, find the railroad track
diagram for {\it expression,\/} and/or look for ``expression'' in the index.
%\bigskip
%$$\vcenter{\halign{\lft{#}\quad&\lft{#}\qquad&\lft{#}\qquad&\lft{#}\quad%
%&\lft{#}\quad&\lft{#}\quad&\lft{#}\quad&\lft{#}\quad&\lft{#}\quad%
%&\lft{#}\quad&\lft{#}\quad&\lft{#}\qquad&\lft{#}\qquad&\lft{#}\qquad&\lft{#}\cr
%program:&&&{\tt PROGRAM}&program&$\bullet$&(&$\bullet$&file&$\bullet$&,&$\bullet$\cr
%&&&&name&&&&name\cr
%\noalign{\bigskip}
%&&&&&&&&&$\bullet$&&)&$\bullet$&;&$\bullet$\cr
%\noalign{\bigskip\bigskip}
%&{\tt BEGIN}&$\bullet$&statement&$\bullet$&;&$\bullet$\cr
%\noalign{\bigskip}
%&&&&$\bullet$&&&{\tt END}&&.\cr}}$$
%\bigskip\bigskip
%\noindent
%which allows the form
%\medskip
%{\obeylines\obeyspaces\let =\ \tt
% PROGRAM {\rm programname (filename)}; BEGIN {\rm statement} END.
%}
%\medskip
In railroad track diagrams, we use rounded boxes for material which must appear
exactly as shown, like `{\tt WRITE}' and `{\tt (}'. In rectangular boxes, we put
descriptive names for some set of allowable forms, usually defined (like
`statement') by another railroad track diagram. To help maintain the distinction,
capital letters are used for words that stand only for themselves, lower case
letters for words that stand for a set of possibilities.
A few more railroad track diagrams sum up what has been said about the form of
programs:
\bigskip
{\baselineskip 10pt
\halign{\qquad\qquad
#\hfil\xskip&\hfil#\hfil\qquad\qquad&\hfil#\hfil\qquad\qquad&\hfil#\hfil\cr
program\cr
&program-&block&{\tt .}\cr
&header\cr
}}
\bigskip\noindent
A program is a {\it block,\/} whatever that is, with a {\it program-header\/}
in front of it and a period after it. Other diagrams will define what a
program-header and a block are.
\bigskip\bigskip
{\baselineskip 10pt
\halign{%\qquad\qquad
#\hfil\xskip
&\hfil#\hfil\qquad\qquad&\hfil#\hfil\qquad\qquad&\hfil#\hfil\qquad\qquad%
&\hfil#\hfil\qquad\qquad&\hfil#\hfil\qquad\qquad&\hfil#\hfil\cr
&&&&{\tt ,}\cr
\noalign{\bigskip}
program-header\cr
&{\tt PROGRAM}&program-&{\tt (}&file-name =&{\tt )}&{\tt ;}\cr
&&name =&&identifier\cr
&&identifer\cr
}}
\bigskip\bigskip\noindent
A program-header is the word ``{\tt PROGRAM}'', then a name for the program,
then (optional) a parenthesized list of file-names separated by commas,
ended with a semicolon.
\bigskip
{\baselineskip 10pt
\halign{\qquad\qquad
#\hfil\qquad\qquad&\hfil#\hfil\qquad\qquad\hfil\cr
block\cr
&declarative-&imperative-\cr
&part&part =\cr
&&compound-\cr
&&statement\cr
}}
\bigskip\noindent
A block has a declarative part and then an imperative part.
\bigskip
{\baselineskip 10pt
\halign{\qquad\qquad
\hfil#\hfil\qquad\qquad&\hfil#\hfil\cr
declarative-\cr
part\cr
&$\bullet$\cr
&$\vdots$\cr
}}
\bigskip\bigskip\noindent
The declarative part may be empty, and will be until we need it.
\bigskip
{\baselineskip 10pt
\halign{\qquad\qquad
\hfil#\hfil\qquad\qquad&\hfil#\hfil\qquad\qquad&\hfil#\hfil\qquad\qquad\hfil\cr
compound&&{\tt ;}\cr
statement\cr
\noalign{\bigskip}
&{\tt BEGIN}&statement&{\tt END}\cr
}}
\bigskip\noindent
A compound-statement in general, and the imperative part of a program in
particular, presents an algorithm. It consists of a series of statements,
separated by semicolons and surrounded by {\tt BEGIN ... END} to mark for
the reader where it begins and ends.
\bigskip
{\baselineskip 10pt
\halign{\qquad\qquad
#\hfil\qquad\qquad&\hfil#\hfil\cr
identifier\cr
&letter\cr
\noalign{\bigskip}
&digit\cr
}}
\bigskip\bigskip\bigskip\noindent
The identifiers used in Pascal as names for programs, files, and several
other entities, must start with a letter and be made of letters and/or digits.
\bigskip
{\baselineskip10pt
\halign{\qquad\qquad
#\hfil\qquad&\hfil#\hfil\qquad&\hfil#\hfil\cr
letter\cr
&&$a$\cr
\noalign{\bigskip}
&&$b$\cr
\noalign{\bigskip}
&&$c$\cr
\noalign{\bigskip}
&$\vdots$\cr
}}
\bigskip\noindent
Self-explanatory.
\smallskip
So, in the {\tt COMETYEAR} program, the first line is a program-header;
the rest, except for the period, is a block. The declarative-part is
empty. The imperative-part contains one {\tt WRITE} statement. The
program-name is {\tt COMETYEAR}, and the sole file-name is {\tt OUTPUT}.
Appendix \_\_\_ contains a complete set of railroad track definitions of Pascal.
You will find it useful even when you don't know the meanings of everything,
by simply ignoring all routes with capitalized words (like `{\tt LABEL}') that have
not yet been explained.
\vfill\eject
\line{\bf Expressions (Formulas): I.\hfill}
Formulas, technically called {\sl expressions\/} in Pascal, are built up from
constants and variables. Constants may be integers (whole numbers) like
$0,1,2,3,\ldots,999,1000,1001,\ldots$ or numbers containing a decimal
point like 98.6, 3.14159, 100.0, 0.0625; if there is a decimal point, it
must be preceded and followed by a digit. Constants may also be named; if
the program begins with a declaration that {\tt PI} stands for 3.14159 and
{\tt E} for 2.71828, then the formula {\tt PI/E} will mean exactly the same as
3.14159/2.71828, which is about 1.15573.
Programs use named variables, like {\tt X} and {\tt MASS} and {\tt GROSSWAGES},
that have numerical values. If the current value of {\tt X} is~5, {\tt X-2}
is an expression with current value~3. Until we have explained how variables
are defined and given values, though, the reader will not be able to use them.
Variables and constants can be connected by {\tt +}, {\tt -}, and {\tt /}, to
get the results of addition, subtraction, and division in the usual way.
The sign for multiplication is the asterisk `{\tt *}', rather than the
usual dot or x-like symbol which tend to be confused with similar ones. The
values of some simple formulas are:
\smallskip
\halign{\qquad\qquad\lft{\tt #}\qquad&\lft{\tt #}\cr
11+3 = 14&3.14+98.6 = 101.74\cr
11-3 = 8&3.14-98.6 = -95.46\cr
11*3 = 33&3.14*98.6 = 309.604\cr
11/3 = 3.6666667&3.14/98.6 = 0.0318458\cr
\quad{\rm (approximately)}&\quad{\rm (approximately)}\cr}
\smallskip\noindent
When the formulas are printed, the results shown with decimal points will appear
in a variant of scientific notation, as
\medskip
{\obeylines
\obeyspaces
\let =\
\tt
3.6666667E+00
1.0174000E+02
-9.5460000E+01
3.0960400E+02
3.1845800E-02
}
\medskip
\noindent
where the last means $3.1845800\times 10↑{-2}$. This ugly but functional
notation
is capable of printing astronomically large or subatomically small numbers
without compromising precision. We will later see other ways to print numbers.
The number of characters used to print a formula depends on whether it is
written in the integer format ({\tt 123456789}) or in scientific notation
({\tt 1.23456789E+08}). In a typical installation, integer and
scientific notation use 12 and~16 characters respectively. In Appendix \_\_\_,
you will find a place to record such parameters for your system. If you are
taking a course, your instructor may provide a completed copy of the Appendix.
When a formula contains more than one operation, Pascal tries to do multiplications
and divisions before additions and subtractions, and otherwise to do operations
from left to right.
\smallskip
\halign{\qquad\qquad\lft{#}\qquad&\lft{#}\qquad&\lft{#}\cr
Formula&Interpretation&Value\cr
\noalign{\smallskip}
{\tt 3+5*7}&{\tt 3+(5*7)}&{\tt 38}\cr
{\tt 3/5*7}&{\tt (3/5)*7}&{\tt 4.2}\cr
{\tt 3-5+7}&{\tt (3-5)+7}&{\tt 5}\cr}
\smallskip\noindent
By use of parentheses around portions which must be done first, the programmer
may control the interpretation:
\smallskip
\halign{\qquad\qquad\lft{#}\qquad&\lft{#}\cr
Formula&Value\cr
\noalign{\smallskip}
{\tt (3+5)*7}&{\tt 56}\cr
{\tt 3/(5*7)}&{\tt 0.0857142}\cr
{\tt 3-(5+7)}&{\tt -9}\cr}
\smallskip
Besides the four operations of arithmetic, Pascal has ways to write many of the
elementary functions of analysis:
\smallskip
\halign{\qquad\qquad\lft{#}\qquad&\lft{#}\cr
Formula&Interpretation\cr
\noalign{\smallskip}
{\tt ABS(X)}&Absolute value, $\left|X\right|$\cr
{\tt SQRT(X)}&Square root, $\sqrt{X}$\cr
{\tt SQR(X)}&Square, $X↑2$\cr
{\tt SIN(X)}&Sine, $\sin X$\cr
{\tt COS(X)}&Cosine, $\cos X$\cr
{\tt EXP(X)}&Exponential, $e↑X$\cr
{\tt LN(X)}&Natural logarithm, $\log↓e X$\cr
{\tt ARCTAN(X)}&Inverse tangent, $\tan↑{-1}X$\cr}
\smallskip
{\rmn
{\narrower\smallskip\noindent
{\bf Example:} if $X$ satisfies the quadratic equation $AX↑2+BX+C=0$, and
$A$, $B$, $C$ are known, $X$~may be computed by the quadratic formula
$${-B\pm\sqrt{B↑2-4AC}\over 2A}$$
In Pascal, one would use
\smallskip}
}
{\rmn
{\obeylines
\obeyspaces
\let =\
\tt
WRITE((-B+SQRT(SQR(B)-4*A*C))/(2*A))
}
}
{\rmn
{\narrower\smallskip\noindent
where only essential parentheses have been used.
\smallskip}
}
{\rmn
{\narrower\smallskip\noindent
{\bf Example:} To print the logarithm of $X$ to the base 2, we would use the
identity $\log↓2X={\ln X\over \ln 2}$; in Pascal,
\smallskip}
}
{\rmn
{\obeylines
\obeyspaces
\let =\
\tt
WRITE(LN(X)/LN(2)).
}
}
[RWF: Move this to a preface.]
The programs and data sets used in this book are available on tape from the
publisher at cost, and may be copied, used, and modified at will. Where
feasible, computer centers should keep them in public files to encourage
student experimentation, and to facilitate assignments entailing modifications.
\bigskip
\line{\copyright 1985 Robert W. Floyd\hfill}
\line{First draft (not published) March 28, l985;
revised October 2, 1985; subsequently revised.\hfil}
\bye